package dao

import (
	"gorm.io/gorm"
	"xlq-demo/app/model"
	"xlq-demo/library/db"
)

type BackendAdminDao struct{}

func NewBackendAdminDao() *BackendAdminDao {
	return new(BackendAdminDao)
}

func (backendAdminDao *BackendAdminDao) Create(backendAdmin *model.BackendAdmin) error {
	return db.GetMaster().Create(backendAdmin).Error
}

func (backendAdminDao *BackendAdminDao) Delete(id interface{}) error {
	return db.GetMaster().Where("id=?", id).Delete(new(model.BackendAdmin)).Error
}

func (backendAdminDao *BackendAdminDao) Update(id string, dataMap map[string]interface{}) error {
	return db.GetMaster().Model(new(model.BackendAdmin)).Where("id=?", id).Updates(dataMap).Error
}

func (backendAdminDao *BackendAdminDao) Find(id interface{}) (*model.BackendAdmin, error) {
	backendAdmin := new(model.BackendAdmin)
	err := db.GetSlave().Where("id=?", id).Take(backendAdmin).Error
	if err == gorm.ErrRecordNotFound {
		err = nil
	}
	return backendAdmin, err
}

func (backendAdminDao *BackendAdminDao) Get(ids []interface{}) ([]model.BackendAdmin, error) {
	var backendAdmins []model.BackendAdmin
	err := db.GetSlave().Find(&backendAdmins, ids).Error
	if err == gorm.ErrRecordNotFound {
		err = nil
	}
	return backendAdmins, err
}

func (backendAdminDao *BackendAdminDao) All() ([]model.BackendAdmin, error) {
	var backendAdmins []model.BackendAdmin
	err := db.GetSlave().Find(&backendAdmins).Error
	if err == gorm.ErrRecordNotFound {
		err = nil
	}
	return backendAdmins, err
}

func (backendAdminDao *BackendAdminDao) FindByWhere(wheres []string, params []interface{}, orders string) (*model.BackendAdmin, error) {
	whereLen := len(wheres)
	backendAdmin := new(model.BackendAdmin)
	db := db.GetSlave()
	for i := 0; i < whereLen; i++ {
		db = db.Where(wheres[i], params[i])
	}
	if orders != "" {
		db = db.Order(orders)
	}
	err := db.Take(&backendAdmin).Error
	if err == gorm.ErrRecordNotFound {
		err = nil
	}
	return backendAdmin, err
}

func (backendAdminDao *BackendAdminDao) GetByWhere(wheres []string, params []interface{}, orders string) ([]model.BackendAdmin, error) {
	whereLen := len(wheres)
	var backendAdmins []model.BackendAdmin
	db := db.GetSlave()
	for i := 0; i < whereLen; i++ {
		db = db.Where(wheres[i], params[i])
	}
	if orders != "" {
		db = db.Order(orders)
	}
	err := db.Find(&backendAdmins).Error
	if err == gorm.ErrRecordNotFound {
		err = nil
	}
	return backendAdmins, err
}

func (backendAdminDao *BackendAdminDao) CountByWhere(wheres []string, params []interface{}) (int64, error) {
	whereLen := len(wheres)
	backendAdmin := new(model.BackendAdmin)
	db := db.GetSlave().Model(backendAdmin)
	for i := 0; i < whereLen; i++ {
		db = db.Where(wheres[i], params[i])
	}
	var total int64
	err := db.Count(&total).Error
	return total, err
}

func (backendAdminDao *BackendAdminDao) ListByWhere(wheres []string, params []interface{}, orders string, offset, limit int) ([]model.BackendAdmin, error) {
	var backendAdmins []model.BackendAdmin
	db := db.GetSlave()
	for i := 0; i < len(wheres); i++ {
		db = db.Where(wheres[i], params[i])
	}
	if orders != "" {
		db = db.Order(orders)
	}
	err := db.Offset(offset).Limit(limit).Find(&backendAdmins).Error
	if err == gorm.ErrRecordNotFound {
		err = nil
	}
	return backendAdmins, err
}

func (backendAdminDao *BackendAdminDao) Raw(sql string, params []interface{}, out interface{}) error {
	err := db.GetSlave().Raw(sql, params...).Scan(out).Error
	if err == gorm.ErrRecordNotFound {
		err = nil
	}
	return err
}

func (backendAdminDao *BackendAdminDao) Exec(sql string, params []interface{}) error {
	return db.GetSlave().Exec(sql, params).Error
}
